package scripts;
import bot.ScriptMethods;

public class SwampMiner extends ScriptMethods implements Runnable {

	public Thread scriptThread;
	public boolean scriptRunning = false;

	public void start() {
		System.gc();
		getAccountSettings();

		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		System.gc();
		scriptRunning = false;
	}

	public void run() {
		while (scriptRunning) {
			int[] rockID = {108, 106, 110, 111};
			int[] itemArray = {155, 153, 154, 160, 159, 158, 157};
			try {
				handleEvents();
				
				if(playerInBounds(108, 695, 119, 706)) {
					if(getInventorySize() < 30){
				       doMine(rockID);
				    } else if (getInventorySize() >= 30) {
				    	walkToBank();
				    }
				}
				
				if(playerInBounds(216, 634, 226, 638 )){
					if(getInventorySize() >= 30){
						doBank(itemArray);	
					} else {
						walkToMine();
					}
				}			
				wait(random(50, 100));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void handleEvents() throws Exception {
		if (welcomeBoxOpen())
			setWelcomeBox();
		if (!isLoggedIn())
			doLogin();
		if (getFatigue() > 80 && !isSleeping()) {
			wait(600);
			useItem(1263);
			wait(600);
		}
		if (getFatigueSleeping() < 10 && isSleeping())
			doSleeping();
		if (getFightMode() != fightMode)
			setFightMode();
	}


	public void doMine(int[] rockID){
	  try{
		for(int i = 0; i < rockID.length; i++){
			int[] rock = getObjectById(rockID[i]);
			while(rock[i] != -1 && !isSleeping() && getInventorySize() < 30) {
				handleEvents();
				atObject(rock[1], rock[2]);
				rock = getObjectById(rockID[i]);
				wait(random(750, 1100));
			}
			wait(200);
		}	
	 } catch (Exception e) {
		e.printStackTrace();
	   }
	}
	
	public void doBank(int[] deposit) {
		try{ 
				while(!bankOpen()){
					openBank();
					wait(1000);
				}
				
				if(bankOpen()){
					for(int i = 0; i < deposit.length; i++){
						if(invContains(deposit[i])){
							depositAll(deposit[i]);
							wait(600);
						}
					} 
					

			}
				closeBank();
				wait(600);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void walkToBank() throws Exception  {
		while(!playerInBounds(216, 634, 226, 638 )){
			wait(600);
		if (ourPlayerX() <= 119 && ourPlayerY() >= 696 && // Top Left
			ourPlayerX() >= 109 && ourPlayerY() <= 706) { // Bottom Right
			walkTo(120, 701, false);
			debug("120, 701");
				}
		if (ourPlayerX() <= 138 && ourPlayerY() >= 694 && // Top Left
			ourPlayerX() >= 119 && ourPlayerY() <= 702) { // Bottom Right
			walkTo(139, 693, false);
			debug("139, 693");
				}
		if (ourPlayerX() <= 153 && ourPlayerY() >= 689 && // Top Left
			ourPlayerX() >= 138 && ourPlayerY() <= 694) { // Bottom Right
			walkTo(154, 689, false);
			debug("154, 689");
				}
		if (ourPlayerX() <= 169 && ourPlayerY() >= 681 && // Top Left
			ourPlayerX() >= 153 && ourPlayerY() <= 690) { // Bottom Right
			walkTo(170, 680, false);
			debug("170, 680");
				}
		if (ourPlayerX() <= 188 && ourPlayerY() >= 662 && // Top Left
			ourPlayerX() >= 169 && ourPlayerY() <= 681) { // Bottom Right
			walkTo(189, 661, false);
			debug("189, 661");
				}
		if (ourPlayerX() <= 210 && ourPlayerY() >= 645 && // Top Left
			ourPlayerX() >= 188 && ourPlayerY() <= 662) { // Bottom Right
			walkTo(211, 644, false);
			debug("211, 644");
				}
		if (ourPlayerX() <= 216 && ourPlayerY() >= 632 && // Top Left
				ourPlayerX() >= 210 && ourPlayerY() <= 645) { // Bottom Right
			walkTo(219, 633, false);
			debug("633, 219");
				}
		
		
		if (ourPlayerX() <= 221 && ourPlayerY() >= 632 && // Top Left
				ourPlayerX() >= 216 && ourPlayerY() <= 633) { // Bottom Right
			int[] bankDoor = getObjectById(64);
			if (bankDoor[0] != -1) {
				atObject(bankDoor[1], bankDoor[2]);
				wait(random(1000, 1200));
			} else {
				walkTo(220, 634, false);
			}
			
		}
	}
	}
	
	public void walkToMine() throws Exception {
		while(!playerInBounds(108, 695, 119, 706)){
			wait(600);
		if (ourPlayerX() <= 221 && ourPlayerY() >= 633 && // Top Left
			ourPlayerX() >= 198 && ourPlayerY() <= 657) { // Bottom Right
			walkTo(197, 658, false);
			debug("197, 658");
				}
		if (ourPlayerX() <= 198 && ourPlayerY() >= 657 && // Top Left
			ourPlayerX() >= 175 && ourPlayerY() <= 676) { // Bottom Right
			walkTo(174, 677, false);
			debug("174, 677");
				}
		if (ourPlayerX() <= 175 && ourPlayerY() >= 676 && // Top Left
			ourPlayerX() >= 146 && ourPlayerY() <= 696) { // Bottom Right
			walkTo(145, 697, false);
			debug("145, 697");
				}
		if (ourPlayerX() <= 146 && ourPlayerY() >= 696 && // Top Left
			ourPlayerX() >= 122 && ourPlayerY() <= 701) { // Bottom Right
			walkTo(121, 701, false);
			debug("121, 701");
				}
		if (ourPlayerX() <= 122 && ourPlayerY() >= 700 && // Top Left
			ourPlayerX() >= 117 && ourPlayerY() <= 702) { // Bottom Right
			walkTo(116, 702, false);
			debug("116, 702");
				}
		
		
		
		if (ourPlayerX() <= 223 && ourPlayerY() >= 634 && // Top Left
			ourPlayerX() >= 216 && ourPlayerY() <= 638) { // Bottom Right
			int[] bankDoor = getObjectById(64);
			if (bankDoor[0] != -1) {
				atObject(bankDoor[1], bankDoor[2]);
				wait(random(1000, 1200));
			} else {
				walkTo(220, 633, false);
				debug("220,633");
			}
		}
			
	}
	}
	public boolean playerInBounds(int x1, int y1, int x2, int y2) throws Exception {
		return ourPlayerX() >= x1 && ourPlayerX() <= x2 && ourPlayerY() >= y1 && ourPlayerY() <= y2;
	}

}